/**
* 头部菜单
*/
<template>
  <div>
    <el-menu class="el-menu-demo" mode="horizontal" background-color="#334157" text-color="#fff" active-text-color="#fff">
      <el-submenu index="2" style="float: right">
        <template slot="title">
          <span v-if="userInfo.nickName">{{userInfo.nickName}}</span>
          <span v-else>用户中心</span>
        </template>
        <el-menu-item @click="openThis" index="2-2">设置基本信息</el-menu-item>
        <el-menu-item @click="edit()" index="2-2">设置登录密码</el-menu-item>
        <el-menu-item @click="exit()" index="2-3">退出</el-menu-item>
      </el-submenu>
    </el-menu>
    <div v-if="editFormVisible">
      <el-dialog title="设置基本信息" :visible.sync="editFormVisible" width="50%" @close="closeDialog">
        <div style="" >
          <el-form :model="formData" ref="form" label-width="100px" class="demo-ruleForm">
            <el-form-item label="昵称" prop="title">
              <div class="ok-box-left">
                <el-input v-model="formData.nickName" style="width: 50%;"></el-input>
              </div>
            </el-form-item>
            <el-form-item label="图片" prop="image">
              <div class="ok-box-left">
                <el-upload class="avatar-uploader" action="http://localhost:8056/public/upload" :show-file-list="false"
                           :on-success="handleAvatarSuccess" style="width: 100px;height: 100px;">
                  <img v-if="formData.image" :src="ok.tool.getImg(formData.image)" class="avatar" alt="">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </div>
            </el-form-item>
          </el-form>
        </div>
        <div class="ok-box-center">
          <el-button size="small" @click="closeDialog">取消</el-button>
          <el-button size="small" type="primary" class="title" @click="editInfo()">保存</el-button>
        </div>
      </el-dialog>
    </div>
  </div>


</template>
<script>
import publicApi from "@/api/publicApi";
import manageApi from "@/api/manage"
import Cookies from "js-cookie";
import ok from "@/assets/ok";
import api from "@/api/manage";
export default {
  name: 'UserTop',
  computed: {
    ok() {
      return ok
    }
  },
  data() {
    return {
      userInfo:{},
      collapsed: true,
      user: {},
      editFormVisible:false,
      formData:{
        id:"",
        nickName:"",
        image:"",
      }
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
    this.user = JSON.parse(localStorage.getItem('userdata'))
    this.getUserInfo();
  },

  methods: {
    // 退出登录
    getUserInfo(){
      this.formData.id = Cookies.get('id');
      api.getUserInfo({id:this.formData.id}).then(res =>{
        this.userInfo = res.result;
      })
    },
    exit() {
      this.$confirm('退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        manageApi.userLogout().then(res =>{
          Cookies.remove("Token");
          this.$router.push("/views/manage/login/loginHome");
          this.$message.success("退出成功！")
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    handleAvatarSuccess(res, file) {
      this.formData.image = res.result;
    },
    openThis(){
      this.formData.nickName = this.userInfo.nickName;
      this.formData.image = this.userInfo.image;
      this.editFormVisible = true;
    },
    editInfo(){
      this.$confirm('是否保存?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.editUserInfo(this.formData).then(res =>{
          if (res){
            this.$message.success("保存成功！");
            this.editFormVisible = false;
            this.getUserInfo();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    edit(){
      this.$prompt('请输入要修改后的密码', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        manageApi.editUserPwd({password:value}).then(res =>{
          this.$message.success("设置成功！")
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        });
      });
    },
    closeDialog(){
      this.editFormVisible=false;
    }
  }
}
</script>
<style scoped>
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px !important;
  text-align: center;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  border: none;
}
.submenu {
  float: right;
}
.buttonimg {
  height: 60px;
  background-color: transparent;
  border: none;
}
.showimg {
  width: 26px;
  height: 26px;
  position: absolute;
  top: 17px;
  left: 17px;
}
.showimg:active {
  border: none;
}
</style>
